﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PsychveyApp1.classes;
using System.Collections;


namespace PsychveyApp1
{
    public partial class CreateSurvey2 : System.Web.UI.Page
    {
        public ArrayList questionPanelArray = new ArrayList();
        public ArrayList questionPanelTypeArray = new ArrayList();
        public int num;

        public Survey survey;
        public UserAccount user;
        public Answer answer;

        public int currentPageNumber;
        public int noQuestionsPerPage;
        public ArrayList questionNumberLinkArray = new ArrayList();
        public ArrayList listOfQuestions;


        protected void Page_Load(object sender, EventArgs e)
        {
           // this.MaintainScrollPositionOnPostBack = true;

            lblUsername.Text = (string)(Session["username"]);

            Survey currentSurvey = (Survey)Session["currentSurvey"];
            UserAccount currentUser = (UserAccount)Session["currentUser"];
            Question currentQuestion = (Question)Session["currentQuestion"];
            //string surveytitle = Request.QueryString["surveytitle"];
            //string typeofsurvey = Request.QueryString["typeofsurvey"];

            //lblTitleOfSurvey.Text = currentSurvey.title;
            
            if (!IsPostBack)
            {
                tbxTitleOfSurvey.Text = DBManager.GetTitleOfSurvey(currentSurvey.surveyID);
                tbxTypeOfSurvey.Text = DBManager.GetTypeOfSurvey(currentSurvey.surveyID);
                surveyExpiryDate.Text = DBManager.GetExpirationDateOfSurvey(currentSurvey.surveyID);
            }

            //surveyExpiryDate.Text = DBManager.GetExpirationDateOfSurvey(currentSurvey.surveyID);

            listOfQuestions = DBManager.GetListOfQuestions(currentSurvey.surveyID, currentUser.userID);
            MultipleChoiceQuestionPanel multipleChoiceQuestionPanel;
            OpenEndedQuestionPanel openEndedQuestionPanel;
            TrueOrFalseQuestion trueOrFalseQuestion;
            LikertQuestion likertQuestion;
            TableRow tr = new TableRow();
            TableCell td = new TableCell();
            
            //for (int i = 0; i < listOfQuestions.Count; i++)
            int start= Convert.ToInt32(Session["currentPageNumber"])*10-10;
            int end = Convert.ToInt32(Session["currentPageNumber"]) * 10;
            if (end > listOfQuestions.Count)
                end = listOfQuestions.Count;
           

            for (int i = start; i < end; i++ )
            {
               
                if (((Question)listOfQuestions[i]).typeOfQuestion.Equals("Multiple Selection Multiple Choice (Customized)"))
                {
                    tr = new TableRow();
                    td = new TableCell();
                    ArrayList listOfAnswers = DBManager.GetListOfAnswer(currentSurvey.surveyID, currentUser.userID, ((Question)listOfQuestions[i]).questionID);
                    // create new questionpanel of currentUser and currentSurvey
                    multipleChoiceQuestionPanel = new MultipleChoiceQuestionPanel(currentUser, currentSurvey, (Question)listOfQuestions[i], listOfAnswers, listOfQuestions.Count, listOfQuestions);
                    multipleChoiceQuestionPanel.btnAddAnswer.Click += new EventHandler(multipleChoiceQuestionPanel.AddAnswer);
                    multipleChoiceQuestionPanel.btnRemoveAnswer.Click += new EventHandler(multipleChoiceQuestionPanel.RemoveAnswer);
                    multipleChoiceQuestionPanel.btnSaveAnswer.Click += new EventHandler(multipleChoiceQuestionPanel.SaveAnswer);
                    multipleChoiceQuestionPanel.btnDeleteQuestion.Click += new EventHandler(multipleChoiceQuestionPanel.RemoveQuestion);
                    multipleChoiceQuestionPanel.btnClear.Click += new EventHandler(multipleChoiceQuestionPanel.ClearForm);
                    multipleChoiceQuestionPanel.dropDownListMC.SelectedIndexChanged += new EventHandler(multipleChoiceQuestionPanel.UpdateQuestionOrder);
                    this.questionPanelArray.Add(multipleChoiceQuestionPanel);
                    this.questionPanelTypeArray.Add("MC");
                    td.Controls.Add(multipleChoiceQuestionPanel);

                    tr.Cells.Add(td);
                    mainTable.Rows.Add(tr);
                }
                else if (((Question)listOfQuestions[i]).typeOfQuestion.Equals("Single Selection Multiple Choice (Customized)"))
                {
                    tr = new TableRow();
                    td = new TableCell();
                    ArrayList listOfAnswers = DBManager.GetListOfAnswer(currentSurvey.surveyID, currentUser.userID, ((Question)listOfQuestions[i]).questionID);
                    // create new questionpanel of currentUser and currentSurvey
                    multipleChoiceQuestionPanel = new MultipleChoiceQuestionPanel(currentUser, currentSurvey, (Question)listOfQuestions[i], listOfAnswers, listOfQuestions.Count, listOfQuestions);
                    multipleChoiceQuestionPanel.btnAddAnswer.Click += new EventHandler(multipleChoiceQuestionPanel.AddAnswer);
                    multipleChoiceQuestionPanel.btnRemoveAnswer.Click += new EventHandler(multipleChoiceQuestionPanel.RemoveAnswer);
                    multipleChoiceQuestionPanel.btnSaveAnswer.Click += new EventHandler(multipleChoiceQuestionPanel.SaveAnswer);
                    multipleChoiceQuestionPanel.btnDeleteQuestion.Click += new EventHandler(multipleChoiceQuestionPanel.RemoveQuestion);
                    multipleChoiceQuestionPanel.btnClear.Click += new EventHandler(multipleChoiceQuestionPanel.ClearForm);
                    multipleChoiceQuestionPanel.dropDownListMC.SelectedIndexChanged += new EventHandler(multipleChoiceQuestionPanel.UpdateQuestionOrder);
                    this.questionPanelArray.Add(multipleChoiceQuestionPanel);
                    this.questionPanelTypeArray.Add("MC");
                    td.Controls.Add(multipleChoiceQuestionPanel);
                    tr.Cells.Add(td);
                    mainTable.Rows.Add(tr);

                }
                else if (((Question)listOfQuestions[i]).typeOfQuestion.Equals("Open Ended"))
                {
                    tr = new TableRow();
                    td = new TableCell();
                    ArrayList listOfAnswers = DBManager.GetListOfAnswer(currentSurvey.surveyID, currentUser.userID, ((Question)listOfQuestions[i]).questionID);

                    //check if listofquestion[i] a open ended question has an answer
                    //Answer answer = DBManager.GetAnswerByQuestionID(currentUser.userID, currentSurvey.surveyID, ((Question)listOfQuestions[i]).questionID);
                    openEndedQuestionPanel = null;
                    //if true, pass in the answer id into the OpenEndedQuestionPanel
                    //if (answer != null)
                    //{

                    openEndedQuestionPanel = new OpenEndedQuestionPanel(currentUser, currentSurvey, (Question)listOfQuestions[i], listOfAnswers, listOfQuestions.Count, listOfQuestions);
                    //}
                    //if false, create a new Answer object
                    //else
                    //{
                    //    Answer answer1 = new Answer();
                    //    answer1.answerText = "";
                    //    answer1.questionID = currentQuestion.questionID;
                    //    answer1.userID = currentUser.userID;
                    //    answer1.surveyID = currentSurvey.surveyID;

                    //    CurrentMaxIDNumber currentMaxAnswerID = DBManager.GetCurrentMaxIDNumber(currentUser.userID);
                    //    answer1.answerID = currentUser.username + "__ANS" + currentMaxAnswerID.currentMaxAnswerID;
                    //    Session["openEndedAnswer"] = answer1;
                    //}


                    // create new questionpanel of currentUser and currentSurvey
                    openEndedQuestionPanel.btnSaveAnswer.Click += new EventHandler(openEndedQuestionPanel.SaveAnswer);
                    openEndedQuestionPanel.btnDeleteQuestion.Click += new EventHandler(openEndedQuestionPanel.RemoveQuestion);
                    openEndedQuestionPanel.btnClear.Click += new EventHandler(openEndedQuestionPanel.ClearForm);
                    openEndedQuestionPanel.dropDownListOE.SelectedIndexChanged += new EventHandler(openEndedQuestionPanel.UpdateQuestionOrder);
                    this.questionPanelArray.Add(openEndedQuestionPanel);
                    this.questionPanelTypeArray.Add("OE");
                    td.Controls.Add(openEndedQuestionPanel);
                    tr.Cells.Add(td);
                    mainTable.Rows.Add(tr);

                }
                else if (((Question)listOfQuestions[i]).typeOfQuestion.Equals("True Or False"))
                {
                    tr = new TableRow();
                    td = new TableCell();
                    ArrayList listOfAnswers = DBManager.GetListOfAnswer(currentSurvey.surveyID, currentUser.userID, ((Question)listOfQuestions[i]).questionID);
                    trueOrFalseQuestion = new TrueOrFalseQuestion(currentUser, currentSurvey, (Question)listOfQuestions[i], listOfAnswers, listOfQuestions.Count, listOfQuestions);
                    // create new questionpanel of currentUser and currentSurvey
                    trueOrFalseQuestion.btnSaveAnswer.Click += new EventHandler(trueOrFalseQuestion.SaveAnswer);
                    trueOrFalseQuestion.btnDeleteQuestion.Click += new EventHandler(trueOrFalseQuestion.RemoveQuestion);
                    trueOrFalseQuestion.btnClear.Click += new EventHandler(trueOrFalseQuestion.ClearForm);
                    trueOrFalseQuestion.dropDownListTrueAndFalse.SelectedIndexChanged += new EventHandler(trueOrFalseQuestion.UpdateQuestionOrder);
                    this.questionPanelArray.Add(trueOrFalseQuestion);
                    this.questionPanelTypeArray.Add("T&F");
                    td.Controls.Add(trueOrFalseQuestion);
                    tr.Cells.Add(td);
                    mainTable.Rows.Add(tr);
                }
                else if (((Question)listOfQuestions[i]).typeOfQuestion.Equals("Likert Question"))
                {
                    tr = new TableRow();
                    td = new TableCell();
                    ArrayList listOfAnswers = DBManager.GetListOfAnswer(currentSurvey.surveyID, currentUser.userID, ((Question)listOfQuestions[i]).questionID);
                    likertQuestion = new LikertQuestion(currentUser, currentSurvey, (Question)listOfQuestions[i], listOfAnswers, listOfQuestions.Count, listOfQuestions);
                    likertQuestion.btnSaveAnswer.Click += new EventHandler(likertQuestion.SaveAnswer);
                    likertQuestion.btnDeleteQuestion.Click += new EventHandler(likertQuestion.RemoveQuestion);
                    likertQuestion.btnClear.Click += new EventHandler(likertQuestion.ClearForm);
                    likertQuestion.dropDownListLikert.SelectedIndexChanged += new EventHandler(likertQuestion.UpdateQuestionOrder);
                    this.questionPanelArray.Add(likertQuestion);
                    this.questionPanelTypeArray.Add("Likert");
                    td.Controls.Add(likertQuestion);
                    tr.Cells.Add(td);
                    mainTable.Rows.Add(tr);
                }
            }

            //creating navigating links
            this.noQuestionsPerPage= 10;
            LinkButton lbt;
            tr = new TableRow();
            td = new TableCell();
            Label lb= new Label();
            lb.Text = "Page Number: ";
            td.Controls.Add(lb);
            //tr.Cells.Add(td);
            for (int i = 0; i <= (listOfQuestions.Count-1) / this.noQuestionsPerPage; i++) 
            {
               // td = new TableCell();
                lb = new Label();
                lb.Text = "|";
                td.Controls.Add(lb);

                if ((i + 1) == Convert.ToInt32(Session["currentPageNumber"]))
                {
                    lb = new Label();
                    lb.Text = Convert.ToString(i + 1);
                    lb.ForeColor = System.Drawing.Color.Red;
                    this.questionNumberLinkArray.Add(lb);
                    td.Controls.Add(lb);
                }
                else
                {
                    lbt = new LinkButton();
                    lbt.Text = Convert.ToString(i + 1);
                    lbt.Click += changePageNumber;
                    this.questionNumberLinkArray.Add(lbt);
                    td.Controls.Add(lbt);
                }
            }
            tr.Cells.Add(td);
            mainTable.Rows.Add(tr);
        }


        public void changePageNumber(Object sender, EventArgs e)
        { 
            Session["currentPageNumber"]= Convert.ToInt32(((LinkButton)sender).Text);
            Response.Redirect("CreateSurvey2.aspx");
        }

        protected void ddlUser_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }



        protected void btnProceed_Click(object sender, EventArgs e)
        {
            if (ddlUser.SelectedIndex == 1)
            {

                Response.Redirect("UserAccountHome.aspx");
            }
            else if (ddlUser.SelectedIndex == 2)
            {
                Response.Redirect("Home.aspx");
            }
        }

        protected void btnConfirmCreateAddQuestionType_Click(object sender, EventArgs e)
        {
     
            Question newQuestion = new Question();
            newQuestion.typeOfQuestion = ddlSelectedTypeOfQuestion.SelectedItem.Text;
            newQuestion.questionsAsked = "";
            newQuestion.surveyID = ((Survey)Session["currentSurvey"]).surveyID;
            newQuestion.userID = ((UserAccount)Session["currentUser"]).userID;
            newQuestion.questionOrder = Convert.ToString(this.listOfQuestions.Count+1);

            if (ddlSelectedTypeOfQuestion.SelectedIndex > 0)
            {
                CurrentMaxIDNumber currentMaxIDNumber = DBManager.GetCurrentMaxIDNumber(((UserAccount)Session["currentUser"]).userID);
                currentMaxIDNumber.currentMaxQuestionID++;
                newQuestion.questionID = ((UserAccount)Session["currentUser"]).username + "__Qns" + currentMaxIDNumber.currentMaxQuestionID;
                DBManager.UpdateCurrentMaxIDNumber(currentMaxIDNumber);
                DBManager.InsertQuestion(newQuestion, (Survey)Session["currentSurvey"], (UserAccount)Session["currentUser"]);
            }
            if (newQuestion.typeOfQuestion.Equals("True Or False"))
            {
                Answer answer = new Answer();
                answer.answerText = "True";
                answer.questionID = newQuestion.questionID;
                answer.userID = ((UserAccount)Session["currentUser"]).userID;
                answer.surveyID = ((Survey)Session["currentSurvey"]).surveyID;


                CurrentMaxIDNumber currentMaxID = DBManager.GetCurrentMaxIDNumber(((UserAccount)Session["currentUser"]).userID);
                currentMaxID.currentMaxAnswerID++;
                answer.answerID = ((UserAccount)Session["currentUser"]).username + "__ANS" + currentMaxID.currentMaxAnswerID;
                DBManager.UpdateCurrentMaxIDNumber(currentMaxID);
                DBManager.InsertAnswer(answer, newQuestion, ((Survey)Session["currentSurvey"]), ((UserAccount)Session["currentUser"]));

                answer = new Answer();
                answer.answerText = "False";
                answer.questionID = newQuestion.questionID;
                answer.userID = ((UserAccount)Session["currentUser"]).userID;
                answer.surveyID = ((Survey)Session["currentSurvey"]).surveyID;

                currentMaxID = DBManager.GetCurrentMaxIDNumber(((UserAccount)Session["currentUser"]).userID);
                currentMaxID.currentMaxAnswerID++;
                answer.answerID = ((UserAccount)Session["currentUser"]).username + "__ANS" + currentMaxID.currentMaxAnswerID;
                DBManager.UpdateCurrentMaxIDNumber(currentMaxID);
                DBManager.InsertAnswer(answer, newQuestion, ((Survey)Session["currentSurvey"]), ((UserAccount)Session["currentUser"]));

            }

            else if (newQuestion.typeOfQuestion.Equals("Open Ended"))
            {
                Answer answer = new Answer();
                answer.answerText = "";
                answer.questionID = newQuestion.questionID;
                answer.userID = ((UserAccount)Session["currentUser"]).userID;
                answer.surveyID = ((Survey)Session["currentSurvey"]).surveyID;

                CurrentMaxIDNumber currentMaxID = DBManager.GetCurrentMaxIDNumber(((UserAccount)Session["currentUser"]).userID);
                currentMaxID.currentMaxAnswerID++;
                answer.answerID = ((UserAccount)Session["currentUser"]).username + "__ANS" + currentMaxID.currentMaxAnswerID;
                DBManager.UpdateCurrentMaxIDNumber(currentMaxID);
                DBManager.InsertAnswer(answer, newQuestion, ((Survey)Session["currentSurvey"]), ((UserAccount)Session["currentUser"]));
            }

            else if (newQuestion.typeOfQuestion.Equals("Likert Question"))
            {
                Answer answer = new Answer();
                answer.answerText = "Strongly Agree";
                answer.questionID = newQuestion.questionID;
                answer.userID = ((UserAccount)Session["currentUser"]).userID;
                answer.surveyID = ((Survey)Session["currentSurvey"]).surveyID;

                CurrentMaxIDNumber currentMaxID = DBManager.GetCurrentMaxIDNumber(((UserAccount)Session["currentUser"]).userID);
                currentMaxID.currentMaxAnswerID++;
                answer.answerID = ((UserAccount)Session["currentUser"]).username + "__ANS" + currentMaxID.currentMaxAnswerID;
                DBManager.UpdateCurrentMaxIDNumber(currentMaxID);
                DBManager.InsertAnswer(answer, newQuestion, ((Survey)Session["currentSurvey"]), ((UserAccount)Session["currentUser"]));

                answer = new Answer();
                answer.answerText = "Agree";
                answer.questionID = newQuestion.questionID;
                answer.userID = ((UserAccount)Session["currentUser"]).userID;
                answer.surveyID = ((Survey)Session["currentSurvey"]).surveyID;

                currentMaxID = DBManager.GetCurrentMaxIDNumber(((UserAccount)Session["currentUser"]).userID);
                currentMaxID.currentMaxAnswerID++;
                answer.answerID = ((UserAccount)Session["currentUser"]).username + "__ANS" + currentMaxID.currentMaxAnswerID;
                DBManager.UpdateCurrentMaxIDNumber(currentMaxID);
                DBManager.InsertAnswer(answer, newQuestion, ((Survey)Session["currentSurvey"]), ((UserAccount)Session["currentUser"]));

                answer = new Answer();
                answer.answerText = "Neutral";
                answer.questionID = newQuestion.questionID;
                answer.userID = ((UserAccount)Session["currentUser"]).userID;
                answer.surveyID = ((Survey)Session["currentSurvey"]).surveyID;

                currentMaxID = DBManager.GetCurrentMaxIDNumber(((UserAccount)Session["currentUser"]).userID);
                currentMaxID.currentMaxAnswerID++;
                answer.answerID = ((UserAccount)Session["currentUser"]).username + "__ANS" + currentMaxID.currentMaxAnswerID;
                DBManager.UpdateCurrentMaxIDNumber(currentMaxID);
                DBManager.InsertAnswer(answer, newQuestion, ((Survey)Session["currentSurvey"]), ((UserAccount)Session["currentUser"]));

                answer = new Answer();
                answer.answerText = "Disagree";
                answer.questionID = newQuestion.questionID;
                answer.userID = ((UserAccount)Session["currentUser"]).userID;
                answer.surveyID = ((Survey)Session["currentSurvey"]).surveyID;

                currentMaxID = DBManager.GetCurrentMaxIDNumber(((UserAccount)Session["currentUser"]).userID);
                currentMaxID.currentMaxAnswerID++;
                answer.answerID = ((UserAccount)Session["currentUser"]).username + "__ANS" + currentMaxID.currentMaxAnswerID;
                DBManager.UpdateCurrentMaxIDNumber(currentMaxID);
                DBManager.InsertAnswer(answer, newQuestion, ((Survey)Session["currentSurvey"]), ((UserAccount)Session["currentUser"]));

                answer = new Answer();
                answer.answerText = "Strongly Disagree";
                answer.questionID = newQuestion.questionID;
                answer.userID = ((UserAccount)Session["currentUser"]).userID;
                answer.surveyID = ((Survey)Session["currentSurvey"]).surveyID;

                currentMaxID = DBManager.GetCurrentMaxIDNumber(((UserAccount)Session["currentUser"]).userID);
                currentMaxID.currentMaxAnswerID++;
                answer.answerID = ((UserAccount)Session["currentUser"]).username + "__ANS" + currentMaxID.currentMaxAnswerID;
                DBManager.UpdateCurrentMaxIDNumber(currentMaxID);
                DBManager.InsertAnswer(answer, newQuestion, ((Survey)Session["currentSurvey"]), ((UserAccount)Session["currentUser"]));
            }

            

            //enable reload of page
            HttpContext.Current.Response.Redirect("CreateSurvey2.aspx");
            //Response.Redirect("CreateSurvey2.aspx");
            Page.MaintainScrollPositionOnPostBack = true;
        }

        protected void AdminLoginShortcut_Click(object sender, EventArgs e)
        {
            Response.Redirect("AdminLogin.aspx");
        }

        protected void MySurveyLinkButton_Click(object sender, EventArgs e)
        {
            Response.Redirect("MySurvey.aspx");
        }

        protected void btnSaveAll_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < this.questionPanelArray.Count; i++)
            {
                if (((String)this.questionPanelTypeArray[i]).Equals("MC"))
                {
                    DBManager.UpdateQuestion(((MultipleChoiceQuestionPanel)questionPanelArray[i]).currentUser.userID, ((MultipleChoiceQuestionPanel)questionPanelArray[i]).currentSurvey.surveyID,
                    ((MultipleChoiceQuestionPanel)questionPanelArray[i]).currentQuestion.questionID,
                    ((MultipleChoiceQuestionPanel)questionPanelArray[i]).tbxQuestion.Text);
                    for (int a = 0; a < ((MultipleChoiceQuestionPanel)questionPanelArray[i]).listOfAnswers.Count; a++)
                    {
                        DBManager.UpdateAnswer(((MultipleChoiceQuestionPanel)questionPanelArray[i]).currentSurvey.surveyID, ((MultipleChoiceQuestionPanel)questionPanelArray[i]).currentUser.userID, ((MultipleChoiceQuestionPanel)questionPanelArray[i]).currentQuestion.questionID,
                        ((Answer)((MultipleChoiceQuestionPanel)questionPanelArray[i]).listOfAnswers[a]).answerID, ((TextBox)((MultipleChoiceQuestionPanel)questionPanelArray[i]).answerTextBoxArray[a]).Text);
                    }
                }
                else if (((String)this.questionPanelTypeArray[i]).Equals("OE"))
                {
                    DBManager.UpdateQuestion(((OpenEndedQuestionPanel)questionPanelArray[i]).currentUser.userID, ((OpenEndedQuestionPanel)questionPanelArray[i]).currentSurvey.surveyID,
                   ((OpenEndedQuestionPanel)questionPanelArray[i]).currentQuestion.questionID,
                   ((OpenEndedQuestionPanel)questionPanelArray[i]).tbxQuestion.Text);
                    
                }
                else if (((String)this.questionPanelTypeArray[i]).Equals("T&F"))
                {
                    DBManager.UpdateQuestion(((TrueOrFalseQuestion)questionPanelArray[i]).currentUser.userID, ((TrueOrFalseQuestion)questionPanelArray[i]).currentSurvey.surveyID,
                   ((TrueOrFalseQuestion)questionPanelArray[i]).currentQuestion.questionID,
                   ((TrueOrFalseQuestion)questionPanelArray[i]).tbxQuestion.Text);
                }
                else if (((String)this.questionPanelTypeArray[i]).Equals("Likert"))
                {
                    DBManager.UpdateQuestion(((LikertQuestion)questionPanelArray[i]).currentUser.userID, ((LikertQuestion)questionPanelArray[i]).currentSurvey.surveyID,
                   ((LikertQuestion)questionPanelArray[i]).currentQuestion.questionID,
                   ((LikertQuestion)questionPanelArray[i]).tbxQuestion.Text);

                }
            }


            if (tbxTitleOfSurvey.Text == "")
            {
               
            }
            else
            {
                Survey survey1 = new Survey();
                survey1.title = tbxTitleOfSurvey.Text;
                survey1.typeOfSurvey = tbxTypeOfSurvey.Text;
                survey1.dateCreated = ((Survey)Session["currentSurvey"]).dateCreated;
                survey1.dateModified = ((Survey)Session["currentSurvey"]).dateModified;
                survey1.userID = ((Survey)Session["currentSurvey"]).userID;
                survey1.typeOfSurvey = ((Survey)Session["currentSurvey"]).typeOfSurvey;
                survey1.surveyID = ((Survey)Session["currentSurvey"]).surveyID;
                survey1.expirationDateOfSurvey = ((Survey)Session["currentSurvey"]).expirationDateOfSurvey;
                survey1.noOfRespondents = ((Survey)Session["currentSurvey"]).noOfRespondents;
                //tbxTitleOfSurvey.Text = DBManager.GetTitleOfSurvey(survey1.surveyID);
                //DBManager.UpdateTitleOfSurvey(survey1.surveyID, survey1.title);
                DBManager.UpdateTitleAndTypeOfSurvey(survey1.surveyID,tbxTitleOfSurvey.Text,tbxTypeOfSurvey.Text);
            }

            DBManager.UpdateDateModified(((Survey)Session["currentSurvey"]).surveyID, DateTime.Now);

            if ((surveyExpiryDate.Text).Equals(""))
            {
                
            }
            else
            {
                if (DBManager.checkIfExpirationDateOfSurveyExists(((Survey)Session["currentSurvey"]).expirationDateOfSurvey, ((Survey)Session["currentSurvey"]).surveyID) == 1)
                {
                    Survey survey1 = new Survey();
                    survey1.title = ((Survey)Session["currentSurvey"]).title;
                    survey1.typeOfSurvey = ((Survey)Session["currentSurvey"]).typeOfSurvey;
                    survey1.dateCreated = ((Survey)Session["currentSurvey"]).dateCreated;
                    survey1.dateModified = ((Survey)Session["currentSurvey"]).dateModified;
                    survey1.userID = ((Survey)Session["currentSurvey"]).userID;
                    survey1.typeOfSurvey = ((Survey)Session["currentSurvey"]).typeOfSurvey;
                    survey1.surveyID = ((Survey)Session["currentSurvey"]).surveyID;
                    survey1.expirationDateOfSurvey = surveyExpiryDate.Text;
                    survey1.expirationDateOfSurvey = DateTime.Now.ToLocalTime().ToString("dd/MM/yyyyTHH:mm");
                    survey1.noOfRespondents = ((Survey)Session["currentSurvey"]).noOfRespondents;
                    DBManager.UpdateExpirationDateOfSurvey(surveyExpiryDate.Text, survey1.surveyID);
                }
                else if (DBManager.checkIfExpirationDateOfSurveyExists(((Survey)Session["currentSurvey"]).expirationDateOfSurvey, ((Survey)Session["currentSurvey"]).surveyID) == 0)
                {
                    Survey survey1 = new Survey();
                    survey1.title = ((Survey)Session["currentSurvey"]).title;
                    survey1.typeOfSurvey = ((Survey)Session["currentSurvey"]).typeOfSurvey;
                    survey1.dateCreated = ((Survey)Session["currentSurvey"]).dateCreated;
                    survey1.dateModified = ((Survey)Session["currentSurvey"]).dateModified;
                    survey1.userID = ((Survey)Session["currentSurvey"]).userID;
                    survey1.typeOfSurvey = ((Survey)Session["currentSurvey"]).typeOfSurvey;
                    survey1.surveyID = ((Survey)Session["currentSurvey"]).surveyID;
                    survey1.expirationDateOfSurvey = surveyExpiryDate.Text;
                    survey1.expirationDateOfSurvey = DateTime.Now.ToLocalTime().ToString("dd/MM/yyyyTHH:mm");
                    survey1.noOfRespondents = ((Survey)Session["currentSurvey"]).noOfRespondents;
                    DBManager.UpdateExpirationDateOfSurvey(surveyExpiryDate.Text, survey1.surveyID);
                }
            }
            Response.Redirect("CreateSurvey2.aspx");
        }

        protected void btnSaveExpiryDate_Click(object sender, EventArgs e)
        {
            DBManager.UpdateExpirationDateOfSurvey(surveyExpiryDate.Text, ((Survey)Session["currentSurvey"]).surveyID);
        }

    }
}

